package com.btashov.datastructure;


public class LinkedListNode {
    private Node head;
    private Node tail;

    //todo: check algorithm in addBefore of util.LinkedList
    public LinkedListNode append(Node n) {
        if (n == null) return this;

        if (head == null) {
            head = tail = n;
            return this;
        }

        tail.setNext(n);
        tail = n;
        return this;
    }

    public Node getHead() {
        return head;
    }

    public Node getTail() {
        return tail;
    }

    public int count() {
        int count = 0;
        Node node = head;
        while (node != null) {
            node = node.getNext();
            count++;
        }
        return count;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Node current = head;
        while (current != null) {
            if (tail != null) {
                sb.append(current.getId()).append("-->");   //todo: refactor - not need to add arrow after the last node
                current = current.getNext();
            }
        }
        return sb.toString();
    }
}
